package com.wq.gamesnack;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wq.gamesnack.utils.PrefUtils;
import com.wq.gamesnack.widget.SnackGameView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.wq.gamesnack.utils.AnimTool.DEF_VIBRATE_DURATION;
import static com.wq.gamesnack.utils.AnimTool.showScalAnim;
import static com.wq.gamesnack.utils.AnimTool.singleVibrate;

/**
 * 贪吃蛇小游戏
 * Created by weiquan on 2018/5/10.
 */

public class GameSnackActivity extends AppCompatActivity {
    public static final String SNACK_HIGHEST = "SnackHighest";
    @BindView(R.id.tvHighestScore)
    TextView tvHighestScore;
    @BindView(R.id.tvCurrentScore)
    TextView tvCurrentScore;
    @BindView(R.id.layScore)
    LinearLayout layScore;
    @BindView(R.id.layTop)
    LinearLayout layTop;
    @BindView(R.id.snackView)
    SnackGameView snackView;
    @BindView(R.id.layGameView)
    RelativeLayout layGameView;
    @BindView(R.id.btnIconLeft)
    ImageView btnIconLeft;
    @BindView(R.id.btnIconTop)
    ImageView btnIconTop;
    @BindView(R.id.btnIconRight)
    ImageView btnIconRight;
    @BindView(R.id.btnIconBottom)
    ImageView btnIconBottom;
    int currentScore = 0, highestScore;
    int baseScore = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_snack);
        ButterKnife.bind(this);
        resetGame();
        highestScore = PrefUtils.getDefaultSp().getInt(SNACK_HIGHEST, 0);
        snackView.setOnEatFoodListener(new SnackGameView.OnEatFoodListener() {
            @Override
            public void eatFood(int speed) {
                currentScore = currentScore + 1 * baseScore;
                setSnackScore();
            }
        });

        SnackGameView.OnGameOverListener onGameOverListener = new SnackGameView.OnGameOverListener() {
            @Override
            public void gameOver(int snackLen, int cellNum) {
                String result = snackLen >= cellNum ? "你赢了,在下佩服" : "游戏结束";//
                singleVibrate(GameSnackActivity.this, DEF_VIBRATE_DURATION);//震动
                new AlertDialog.Builder(GameSnackActivity.this).setMessage(result)
                        .setNegativeButton("新游戏", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                resetGame();
                            }
                        }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                }).create().show();
            }
        };
        snackView.setOnGameOverListener(
                onGameOverListener);
        setSnackScore();
    }

    /**
     * 设置分数
     */
    void setSnackScore() {
        tvCurrentScore.setText(String.format("当前积分: %d", currentScore));
        PrefUtils.set(SNACK_HIGHEST, highestScore = Math.max(currentScore, highestScore));
        tvHighestScore.setText(String.format("最高积分: %d", highestScore));
    }

    /**
     * 初始化/重置游戏
     */
    private void resetGame() {
        snackView.initGame();
        currentScore = 0;
        setSnackScore();
    }


    @OnClick({R.id.btnIconLeft, R.id.btnIconTop, R.id.btnIconRight, R.id.btnIconBottom})
    public void onClick(View view) {
        showScalAnim(view);//点击动画
        singleVibrate(this, DEF_VIBRATE_DURATION);//震动
        switch (view.getId()) {
            case R.id.btnIconLeft:
                snackView.turnTo(SnackGameView.S_LEFT);
                break;
            case R.id.btnIconTop:
                snackView.turnTo(SnackGameView.S_TOP);
                break;
            case R.id.btnIconRight:
                snackView.turnTo(SnackGameView.S_RIGHT);
                break;
            case R.id.btnIconBottom:
                snackView.turnTo(SnackGameView.S_BOTTOM);
                break;
        }
    }
}
